﻿using CommunityToolkitMvvmDemo.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using System.Configuration;
using System.Data;
using System.Windows;

namespace CommunityToolkitMvvmDemo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public new static App Current => (App)Application.Current;
        public IServiceProvider Services { get; }

        private static string language;
        public static string Language
        {
            get { return language; }
            set
            {
                if (language != value)
                {
                    language = value;

                    List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>();
                    foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries)
                    {
                        dictionaryList.Add(dictionary);
                    }

                    string requestedLanguage = string.Format(@"\Assets\Languages\StringResource.{0}.xaml", Language);  //   \\Assets\\Languages\\StringResource.zh-CN.xaml
                   //ResourceDictionary resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedLanguage));

                    ResourceDictionary resourceDictionary = dictionaryList.Where(s => s.Source != null).FirstOrDefault(d =>  d.Source.OriginalString.Equals(requestedLanguage));

                    if (resourceDictionary == null)
                    {
                        requestedLanguage = @"\Assets\Languages\StringResource.en-US.xaml";
                        resourceDictionary = dictionaryList.FirstOrDefault(d => d.Source.OriginalString.Equals(requestedLanguage));
                    }

                    if (resourceDictionary != null)
                    {
                        Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
                        Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
                    }
                }
            }
        }

        public App()
        {
            // 将程序的语言改成 zh-CH
            //Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("zh-CN");
            //Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CN");

            Services = ConfigureServices();
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // 初始化语言    language = "en-US";
            Language = string.IsNullOrEmpty(Language) ? "en-US" : Language;

            // 配置窗口跳转
            Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
            if (new LoginWindow().ShowDialog() == true)
            {
                //new MainWindow().Show();
                new HandyControlWindow().Show();

                Application.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
            }
            //App.Current.Shutdown();
        }

        private IServiceProvider? ConfigureServices()
        {
            var services = new ServiceCollection();

            //services.AddSingleton<IFilesService, FilesService>();

            services.AddSingleton<ValidationRuleViewModel>();
        
           
            services.AddSingleton<ConverterViewModel>();
            services.AddSingleton<RegisterViewModel>();

            services.AddSingleton<HandyControlViewModel>();

            services.AddSingleton<LoginViewModel>();
            services.AddSingleton<MainViewModel>();
            services.AddSingleton<HomePageViewModel>();
            services.AddSingleton<CourseCenterViewModel>();

            return services.BuildServiceProvider();
        }
    }

}
